Imports BusinessLogicLayer.BLMain
Imports BusinessLogicLayer.BLException
Imports DataAccessLayer
Imports DataAccessLayer.DLMain
Imports UniversalLayer
Imports UniversalLayer.ULMain
Imports UniversalLayer.ULDataHandler

Friend NotInheritable Class frmSettings

    Dim _DLMain As DLMain
    'Dim _GUIPopulator As EntertainmentManager.GUIPopulator
    'Dim myFormMain As EntertainmentManager.frmEntertainmentMgr
    Public dtSettingsMain As DataTable

    ''' <summary>
    ''' Creates new instance and initializes all controls of Settings Form
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New(ByRef myFormMain As EntertainmentManager.frmEntertainmentMgr)
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dim _GUIPopulator As EntertainmentManager.GUIPopulator
        'Dim _DLMain As DLMain

        Try
            '_DLMain = New DLMain
            _GUIPopulator = New EntertainmentManager.GUIPopulator(Me)
            CopyDataTable(myFormMain.dtSettingsMain, dtSettingsMain)
            '_GUIPopulator.InitializeFormSettings(Me, dtSettings)

            If CBool(DLSettings.FetchOneSetting(dtSettingsMain, "SQL", "Formatted", "")) Then
                rtbSearchSQL.Text = _GUIPopulator.FormatSQL(myFormMain.sSearchSQL)
                rtbSuggestionSQL.Text = _GUIPopulator.FormatSQL(myFormMain.sSuggestionSQL)
                rtbWatchlistSQL.Text = _GUIPopulator.FormatSQL(myFormMain.sWatchlistSQL)
            Else
                rtbSearchSQL.Text = myFormMain.sSearchSQL
                rtbSuggestionSQL.Text = myFormMain.sSuggestionSQL
                rtbWatchlistSQL.Text = myFormMain.sWatchlistSQL
            End If

            'RKD: Initialize all controls from dtSettings
            'design_settings_from_EXCEL_and_dtSettings()

        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmSettings.New")
        Finally
            _DLMain = Nothing
            _GUIPopulator = Nothing
        End Try
    End Sub

    Private Sub frmSettings_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'RKD() ':: assign all settings from frmEntertainmentMgr.dtSettings


        '' ''chklstClipboardCols.Items.AddRange(DLMain.sFieldsAll.Split(","c))

        '' ''For i As Integer = 0 To chklstClipboardCols.Items.Count - 1
        '' ''    If chklstClipboardCols.GetItemChecked(i) Then
        '' ''        MessageBox.Show(chklstClipboardCols.Items(i).ToString)
        '' ''        chklstClipboardCols.SetItemChecked(i, True)
        '' ''        chklstClipboardCols.SetItemCheckState(i, CheckState.Checked)
        '' ''    End If
        '' ''Next

        Dim _GUIPopulator As EntertainmentManager.GUIPopulator
        Try
            _GUIPopulator = New EntertainmentManager.GUIPopulator(Me)
            _GUIPopulator.InitializeFormSettings(Me)

            rtbSearchSQL.BorderStyle = BorderStyle.None
            rtbSuggestionSQL.BorderStyle = BorderStyle.None
            rtbWatchlistSQL.BorderStyle = BorderStyle.None
            btnFormatSearchSQL.Enabled = False
            btnFormatSuggestionSQL.Enabled = False
            btnFormatWatchlistSQL.Enabled = False
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmSettings.frmSettings_Load")
        Finally
            _GUIPopulator = Nothing
        End Try
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        'RKD: Validate
        Dim sErrorMessage As String = ""
        Dim _GUIPopulator As EntertainmentManager.GUIPopulator

        Try
            _GUIPopulator = New EntertainmentManager.GUIPopulator(Me)
            If _GUIPopulator.ValidateSettings(Me, sErrorMessage) Then
                'RKD: Write to DB
                MessageBox.Show("No errors found. Add code to write to DB.", "VALID data..")
            ElseIf Not IsEmpty(sErrorMessage) Then
                sErrorMessage = "Settings could not be saved. Please correct the data and Save again.." & vbCrLf & vbCrLf & sErrorMessage
                MessageBox.Show(sErrorMessage, "Error in data..")
            End If

            Me.Close()
            'Me.Hide()
            'frmEntertainmentMgr.Show()

        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmSettings.btnSave_Click")
            MessageBox.Show("Error: " & ex.Message & vbCrLf & vbCrLf & "Details:" & vbCrLf & ex.ToString, "Error: EntertainmentManager.frmSettings.btnSave_Click", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            _GUIPopulator = Nothing
        End Try
    End Sub

    Private Sub btnDiscard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDiscard.Click
        Me.Close()
        Me.Dispose()
    End Sub

    Private Sub btnFormatSearchSQL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFormatSearchSQL.Click
        Dim _GUIPopulator As EntertainmentManager.GUIPopulator
        Try
            _GUIPopulator = New EntertainmentManager.GUIPopulator(Me)
            rtbSearchSQL.Text = _GUIPopulator.FormatSQL(rtbSearchSQL.Text)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmSettings.btnFormatSearchSQL_Click")
        Finally
            _GUIPopulator = Nothing
        End Try
    End Sub

    Private Sub btnFormatSuggestionSQL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFormatSuggestionSQL.Click
        Dim _GUIPopulator As EntertainmentManager.GUIPopulator
        Try
            _GUIPopulator = New EntertainmentManager.GUIPopulator(Me)
            rtbSuggestionSQL.Text = _GUIPopulator.FormatSQL(rtbSuggestionSQL.Text)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmSettings.btnFormatSuggestionSQL_Click")
        Finally
            _GUIPopulator = Nothing
        End Try
    End Sub

    Private Sub btnFormatWatchlistSQL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFormatWatchlistSQL.Click
        Dim _GUIPopulator As EntertainmentManager.GUIPopulator
        Try
            _GUIPopulator = New EntertainmentManager.GUIPopulator(Me)
            rtbWatchlistSQL.Text = _GUIPopulator.FormatSQL(rtbWatchlistSQL.Text)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmSettings.btnFormatWatchlistSQL_Click")
        Finally
            _GUIPopulator = Nothing
        End Try
    End Sub

    Private Sub numNDiscsMin_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numNDiscsMin.ValueChanged
        Dim _GUIPopulator As EntertainmentManager.GUIPopulator
        Try
            _GUIPopulator = New EntertainmentManager.GUIPopulator(Me)
            _GUIPopulator.AdjustRanges(Me, eRangeMeta.nDiscs, eRangeType.Minimum)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmSettings.numNDiscsMin_ValueChanged")
        Finally
            _GUIPopulator = Nothing
        End Try
    End Sub

    Private Sub numNDiscsDef_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numNDiscsDef.ValueChanged
        Dim _GUIPopulator As EntertainmentManager.GUIPopulator
        Try
            _GUIPopulator = New EntertainmentManager.GUIPopulator(Me)
            _GUIPopulator.AdjustRanges(Me, eRangeMeta.nDiscs, eRangeType.Default)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmSettings.numNDiscsDef_ValueChanged")
        Finally
            _GUIPopulator = Nothing
        End Try
    End Sub

    Private Sub numNDiscsMax_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numNDiscsMax.ValueChanged
        Dim _GUIPopulator As EntertainmentManager.GUIPopulator
        Try
            _GUIPopulator = New EntertainmentManager.GUIPopulator(Me)
            _GUIPopulator.AdjustRanges(Me, eRangeMeta.nDiscs, eRangeType.Maximum)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmSettings.numNDiscsMax_ValueChanged")
        Finally
            _GUIPopulator = Nothing
        End Try
    End Sub

    Private Sub numRelYrMin_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numRelYrMin.ValueChanged
        Dim _GUIPopulator As EntertainmentManager.GUIPopulator
        Try
            _GUIPopulator = New EntertainmentManager.GUIPopulator(Me)
            _GUIPopulator.AdjustRanges(Me, eRangeMeta.ReleaseYear, eRangeType.Minimum)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmSettings.numRelYrMin_ValueChanged")
        Finally
            _GUIPopulator = Nothing
        End Try
    End Sub

    Private Sub numRelYrDef_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numRelYrDef.ValueChanged
        Dim _GUIPopulator As EntertainmentManager.GUIPopulator
        Try
            _GUIPopulator = New EntertainmentManager.GUIPopulator(Me)
            _GUIPopulator.AdjustRanges(Me, eRangeMeta.ReleaseYear, eRangeType.Default)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmSettings.numRelYrDef_ValueChanged")
        Finally
            _GUIPopulator = Nothing
        End Try
    End Sub

    Private Sub numRelYrMax_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numRelYrMax.ValueChanged
        Dim _GUIPopulator As EntertainmentManager.GUIPopulator
        Try
            _GUIPopulator = New EntertainmentManager.GUIPopulator(Me)
            _GUIPopulator.AdjustRanges(Me, eRangeMeta.ReleaseYear, eRangeType.Maximum)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmSettings.numRelYrMax_ValueChanged")
        Finally
            _GUIPopulator = Nothing
        End Try
    End Sub

    Private Sub numRuntimeMin_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numRuntimeMin.ValueChanged
        Dim _GUIPopulator As EntertainmentManager.GUIPopulator
        Try
            _GUIPopulator = New EntertainmentManager.GUIPopulator(Me)
            _GUIPopulator.AdjustRanges(Me, eRangeMeta.RunTime, eRangeType.Minimum)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmSettings.numRuntimeMin_ValueChanged")
        Finally
            _GUIPopulator = Nothing
        End Try
    End Sub

    Private Sub numRuntimeDef_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numRuntimeDef.ValueChanged
        Dim _GUIPopulator As EntertainmentManager.GUIPopulator
        Try
            _GUIPopulator = New EntertainmentManager.GUIPopulator(Me)
            _GUIPopulator.AdjustRanges(Me, eRangeMeta.RunTime, eRangeType.Default)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmSettings.numRuntimeDef_ValueChanged")
        Finally
            _GUIPopulator = Nothing
        End Try
    End Sub

    Private Sub numRuntimeMax_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numRuntimeMax.ValueChanged
        Dim _GUIPopulator As EntertainmentManager.GUIPopulator
        Try
            _GUIPopulator = New EntertainmentManager.GUIPopulator(Me)
            _GUIPopulator.AdjustRanges(Me, eRangeMeta.RunTime, eRangeType.Maximum)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmSettings.numRuntimeMax_ValueChanged")
        Finally
            _GUIPopulator = Nothing
        End Try
    End Sub

    Private Sub numTVSnsMin_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numTVSnsMin.ValueChanged
        Dim _GUIPopulator As EntertainmentManager.GUIPopulator
        Try
            _GUIPopulator = New EntertainmentManager.GUIPopulator(Me)
            _GUIPopulator.AdjustRanges(Me, eRangeMeta.TVSeason, eRangeType.Minimum)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmSettings.numTVSnsMin_ValueChanged")
        Finally
            _GUIPopulator = Nothing
        End Try
    End Sub

    Private Sub numTVSnsDef_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numTVSnsDef.ValueChanged
        Dim _GUIPopulator As EntertainmentManager.GUIPopulator
        Try
            _GUIPopulator = New EntertainmentManager.GUIPopulator(Me)
            _GUIPopulator.AdjustRanges(Me, eRangeMeta.TVSeason, eRangeType.Default)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmSettings.numTVSnsDef_ValueChanged")
        Finally
            _GUIPopulator = Nothing
        End Try
    End Sub

    Private Sub numTVSnsMax_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numTVSnsMax.ValueChanged
        Dim _GUIPopulator As EntertainmentManager.GUIPopulator
        Try
            _GUIPopulator = New EntertainmentManager.GUIPopulator(Me)
            _GUIPopulator.AdjustRanges(Me, eRangeMeta.TVSeason, eRangeType.Maximum)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmSettings.numTVSnsMax_ValueChanged")
        Finally
            _GUIPopulator = Nothing
        End Try
    End Sub

    Private Sub numTVEpsMin_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numTVEpsMin.ValueChanged
        Dim _GUIPopulator As EntertainmentManager.GUIPopulator
        Try
            _GUIPopulator = New EntertainmentManager.GUIPopulator(Me)
            _GUIPopulator.AdjustRanges(Me, eRangeMeta.TVEpisode, eRangeType.Minimum)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmSettings.numTVEpsMin_ValueChanged")
        Finally
            _GUIPopulator = Nothing
        End Try
    End Sub

    Private Sub numTVEpsDef_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numTVEpsDef.ValueChanged
        Dim _GUIPopulator As EntertainmentManager.GUIPopulator
        Try
            _GUIPopulator = New EntertainmentManager.GUIPopulator(Me)
            _GUIPopulator.AdjustRanges(Me, eRangeMeta.TVEpisode, eRangeType.Default)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmSettings.numTVEpsDef_ValueChanged")
        Finally
            _GUIPopulator = Nothing
        End Try
    End Sub

    Private Sub numTVEpsMax_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numTVEpsMax.ValueChanged
        Dim _GUIPopulator As EntertainmentManager.GUIPopulator
        Try
            _GUIPopulator = New EntertainmentManager.GUIPopulator(Me)
            _GUIPopulator.AdjustRanges(Me, eRangeMeta.TVEpisode, eRangeType.Maximum)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmSettings.numTVEpsMax_ValueChanged")
        Finally
            _GUIPopulator = Nothing
        End Try
    End Sub
End Class